package com.nado.iims2.blog.action.service;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.nado.extensions.pagination.PaginationInfo;
import com.nado.iims2.blog.bo.service.BannerBO;
import com.nado.iims2.blog.model.service.Banner;
import com.nado.iims2.blog.model.service.BannerThema;
import com.nado.iims2.blog.util.CommonUtil;

/**
 * Handles requests for the application banner page.
 */
@Controller
public class BannerController {
	/** The logger. */
	private static final Logger logger = LoggerFactory.getLogger(BannerController.class);
	
	@Autowired
	private BannerBO bannerBO = null;
	
	@RequestMapping(value = "/service/banner", method = RequestMethod.GET)
	public String list(HttpServletRequest request, Model model) {
		logger.info("/service/banner!");
		int page = NumberUtils.toInt(request.getParameter("page"), 1);
		String progressYn = StringUtils.defaultIfEmpty(request.getParameter("progressYn"), "N");
		int bnnrThmSeq = NumberUtils.toInt(request.getParameter("bnnrThmSeq"), 0);
		
		logger.info("/service/banner page is " + page);
		logger.info("/service/banner progressYn is " + progressYn);
		logger.info("/service/banner bnnrThmSeq is " + bnnrThmSeq);
		
		PaginationInfo paginationInfo = null;
		List<BannerThema> bnnrThmList = new ArrayList<BannerThema>();
		List<Banner> bnnrList = new ArrayList<Banner>();
		
		// Gets the banner thema list
		bnnrThmList = bannerBO.getBannerThemaList();
		
		// Get banner thema no.
		if(bnnrThmSeq == 0) {
			bnnrThmSeq = ((BannerThema)bnnrThmList.get(0)).getBnnrThemaNo();
		}
		
		int count = bannerBO.getBannerListCount(bnnrThmSeq, progressYn);
		paginationInfo = new PaginationInfo("default", request, count);
		// Gets the banner list.
		bnnrList = bannerBO.getBannerList(bnnrThmSeq, progressYn, 
				CommonUtil.calcOffset(page, paginationInfo.getPageSize()), 
				paginationInfo.getPageSize());
		
		model.addAttribute("progressYn", progressYn);
		model.addAttribute("bnnrThmSeq", bnnrThmSeq);
		model.addAttribute("bnnrThmList", bnnrThmList);
		model.addAttribute("bnnrCount", count);
		model.addAttribute("bnnrList", bnnrList);
		model.addAttribute("paginate", paginationInfo);
		return "/service/bannerList";
	}
	
	@RequestMapping(value = "/service/banner/form/{thmNo}", method = RequestMethod.GET)
	public String form(@PathVariable int thmNo, Model model) {
		logger.info("/service/banner/form/" + thmNo);
		BannerThema bannerThema = bannerBO.getBannerThemaByNo(thmNo);
		Banner banner = new Banner();
		
		model.addAttribute(bannerThema);
		model.addAttribute(banner);
		return "/service/bannerForm";
	}
	
	@RequestMapping(value = "/service/banner/write", method = RequestMethod.POST)
	public String write() {
		
		
		return "redirect:/service/banner";
	}
}
